<?php
/*
 * This file is part of the Geek-Zoo Projects.
 *
 * @copyright (c) 2010 Geek-Zoo Projects More info http://www.geek-zoo.com
 * @license http://opensource.org/licenses/gpl-2.0.php The GNU General Public License
 * @author xuanyan <xuanyan@geek-zoo.com>
 *
 */

define('ROOT_PATH', dirname(__FILE__));
require_once ROOT_PATH.'/Library/__init__.php';
require_once ROOT_PATH.'/config.php';

if (IS_SINA) {
    $site_dir = '/manage';
    if (isset($_SERVER['SCRIPT_URL'])) {
        $_SERVER['REDIRECT_URL'] = substr($_SERVER['SCRIPT_URL'], strlen($site_dir));
    }
} else {
    $site_dir = '';
    if ($pos = strpos($_SERVER['PHP_SELF'], '/index.php')) {
        $site_dir = substr($_SERVER['PHP_SELF'], 0, $pos);
        if (isset($_SERVER['REDIRECT_URL'])) {
            $_SERVER['REDIRECT_URL'] = substr($_SERVER['REDIRECT_URL'], strlen($site_dir));
        }
    }
}


define('SITE_URL', 'http://'.$_SERVER['HTTP_HOST'].$site_dir);

require_once ROOT_PATH.'/flash_video.php';
//require_once ROOT_PATH.'/Abstracts/front.php';

Database::$instance = new DatebaseRWProxy(Database::connect(Config::get('db_r')), Database::connect(Config::get('db')), array(
    'SET character_set_connection=utf8, character_set_results=utf8, character_set_client=binary',
    "SET sql_mode=''"
));


Model::$dir = ROOT_PATH.'/Model';
// Model::$dbPrefix = array('admin_'=>'*');
// Model::$dbPrefix = array('admin_'=>array('admin', 'admin_group', 'attribute', 'attribute_value', 'menu', 'model', 'resource', 'storage_data', 'storage_index', 'storage_mate'));
try {
    if (($result = Controller::dispatch(@$_SERVER['REDIRECT_URL'], ROOT_PATH.'/Controller')) && Controller::$format == 'json') {
        echo json_encode($result);
    }
} catch (Exception $e) {
    // out put 404 page
    if ($e->getCode() == 404) {
        header("HTTP/1.0 404 Not Found");
        readfile(ROOT_PATH.'/404.html');
        exit;
    }
    die($e);
}

$_SESSION['LAST_URL'] = SITE_URL.$_SERVER['REQUEST_URI'];

?>
